package com.app.couponsharelife.ui.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import com.app.couponsharelife.data.bean.*
import com.app.couponsharelife.data.remote.RemoteDataSource

/**
 * Created by guluwa on 2018/11/30.
 */
class SortViewModel : BaseViewModel() {

    private var getPrimarySortsFresh = MutableLiveData<FreshBean>()

    private var getPrimarySortsResult: LiveData<ViewDataBean<ResultBean<PrimarySortBean>>>? = null

    fun getPrimarySorts(): LiveData<ViewDataBean<ResultBean<PrimarySortBean>>>? {
        if (getPrimarySortsResult == null) {
            getPrimarySortsResult = Transformations.switchMap(getPrimarySortsFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getPrimarySorts(it.map)
                } else {
                    null
                }
            }
        }
        return getPrimarySortsResult!!
    }

    fun refreshGetPrimarySorts(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) getPrimarySortsFresh.value = FreshBean(map, isFresh)
    }

    private var getSecondarySortsFresh = MutableLiveData<FreshBean>()

    private var getSecondarySortsResult: LiveData<ViewDataBean<ResultBean<SecondarySortBean>>>? = null

    fun getSecondarySorts(): LiveData<ViewDataBean<ResultBean<SecondarySortBean>>>? {
        if (getSecondarySortsResult == null) {
            getSecondarySortsResult = Transformations.switchMap(getSecondarySortsFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getSecondarySorts(it.map)
                } else {
                    null
                }
            }
        }
        return getSecondarySortsResult!!
    }

    fun refreshGetSecondarySorts(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) getSecondarySortsFresh.value = FreshBean(map, isFresh)
    }
}